<template>
  <div class="app-container">
    <el-button type="primary" @click="handleAdd">创建活动</el-button>

    <el-table :data="fullReduceList" style="width: 100%;margin-top:30px;" stripe>
      <el-table-column 
        prop="name" 
        align="center" 
        label="活动名称">
      </el-table-column>
      <el-table-column 
        prop="fullPrice" 
        align="center" 
        label="满价">
      </el-table-column>
      <el-table-column 
        prop="reducePrice" 
        align="center" 
        label="减价">
      </el-table-column>
      <el-table-column 
        prop="starat" 
        align="center" 
        label="活动开始时间">
      </el-table-column>
      <el-table-column 
        prop="endat" 
        align="center" 
        label="活动结束时间">
      </el-table-column>
      <el-table-column 
        prop="status" 
        align="center" 
        label="状态">
      </el-table-column>
      <el-table-column 
        prop="createat" 
        align="center" 
        label="创建时间">
      </el-table-column>

      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleEdit(scope)">编辑</el-button>
          <el-button type="text" size="small" @click="handleStatus(scope)">设置商品</el-button>
          <el-button type="text" size="small" @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="dialogVisible" :title="dialogType==='edit'?'编辑活动':'创建活动'">
      <el-form :model="fullReduce" label-width="80px" label-position="left">
        <el-form-item label="菜单名称">
          <el-input v-model="fullReduce.title" placeholder="菜单名称" />
        </el-form-item>
        <el-form-item label="满价">
          <el-input v-model="fullReduce.fullPrice" placeholder="满价" />
        </el-form-item>
        <el-form-item label="减价">
          <el-input v-model="fullReduce.reducePrice" placeholder="减价" />
        </el-form-item>
        <el-form-item label="开始时间">
          <el-input v-model="fullReduce.starat" placeholder="减价" />
        </el-form-item>
        <el-form-item label="结束时间">
          <el-input v-model="fullReduce.endat" placeholder="减价" />
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmSubmit">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import path from 'path'
import { deepClone } from '@/utils'
import { getRoutes, getRoles, addRole, deleteRole, updateRole } from '@/api/role'
import { getMenus, getParents, addMenu, updateMenu, delMenu } from '@/api/permission'

const defaultRole = {
  key: '',
  name: '',
  description: '',
  routes: []
}

export default {
  data() {
    return {
      fullReduce: {
        id: '',
        name: '',
        fullPrice: '',
        reducePrice: '',
        starat: '',
        endat: ''
      },
      fullReduceList: [],
      dialogVisible: false,
      dialogType: 'new',
      checkStrictly: false,
      defaultProps: {
        children: 'children',
        label: 'title'
      }
    }
  },
  computed: {
    routesData() {
      return this.routes
    }
  },
  created() {
    // Mock: get all routes and roles list from server
    this.getMenus()
    this.getParentList()
  },
  methods: {
    async getMenus() {
      const res = await getMenus()
      console.log("getMenus",res)
      // this.serviceRoutes = res.data
      this.menuList = res.data
    },
    async getParentList() {
      const res = await getParents()
      console.log("getParentList",res)
      this.parentList = res.data
    },

    // Reshape the routes structure so that it looks the same as the sidebar
    generateRoutes(routes, basePath = '/') {
      const res = []

      for (let route of routes) {
        // skip some route
        if (route.hidden) { continue }

        const onlyOneShowingChild = this.onlyOneShowingChild(route.children, route)

        if (route.children && onlyOneShowingChild && !route.alwaysShow) {
          route = onlyOneShowingChild
        }

        const data = {
          path: path.resolve(basePath, route.path),
          title: route.meta && route.meta.title

        }

        // recursive child routes
        if (route.children) {
          data.children = this.generateRoutes(route.children, data.path)
        }
        res.push(data)
      }
      return res
    },
    generateArr(routes) {
      let data = []
      routes.forEach(route => {
        data.push(route)
        if (route.children) {
          const temp = this.generateArr(route.children)
          if (temp.length > 0) {
            data = [...data, ...temp]
          }
        }
      })
      return data
    },
    handleAdd() {
      // console.log("menu",this.menu)
      // console.log("menuAdd",this.menuAdd)
      this.menu = {}
      this.dialogType = 'new'
      this.dialogVisible = true
    },
    handleEdit(scope) {
      const editMenu = scope.row
      this.menu.id = editMenu.id
      this.menu.title = editMenu.title
      if(editMenu.parent != null){
        this.menu.parentId = editMenu.parent.id
      }
      this.menu.name = editMenu.name
      this.dialogType = 'edit'
      this.dialogVisible = true
    },
    handleDelete(scope) {
      this.$confirm('确认删除菜单：'+scope.row.title+'?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await delMenu(scope.row.id)
          this.$message({
            type: 'success',
            message: '删除菜单成功'
          })
          this.getMenus()
        })
        .catch(() => { 
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
         })
    },

    async confirmSubmit() {
      const isEdit = this.dialogType === 'edit'

      if (isEdit) {
        await updateMenu(this.menu)
        this.$message({
            type: 'success',
            message: '更新菜单成功!'
          })
      } else {
        await addMenu(this.menu)
        this.$message({
            type: 'success',
            message: '创建菜单成功!'
          })
      }

      this.dialogVisible = false
      this.getMenus()

    },
    // reference: src/view/layout/components/Sidebar/SidebarItem.vue
    onlyOneShowingChild(children = [], parent) {
      let onlyOneChild = null
      const showingChildren = children.filter(item => !item.hidden)

      // When there is only one child route, the child route is displayed by default
      if (showingChildren.length === 1) {
        onlyOneChild = showingChildren[0]
        onlyOneChild.path = path.resolve(parent.path, onlyOneChild.path)
        return onlyOneChild
      }

      // Show parent if there are no child route to display
      if (showingChildren.length === 0) {
        onlyOneChild = { ... parent, path: '', noShowingChildren: true }
        return onlyOneChild
      }

      return false
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .roles-table {
    margin-top: 30px;
  }
  .permission-tree {
    margin-bottom: 30px;
  }
}
</style>
